package com.selfdriving.wl.activity;

import java.util.List;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.activity.AboutMeMyArticleDetails;
import com.selfdriving.adapter.FeatureDiaryAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.MyArticle;
import com.selfdriving.bean.MyArticleDetails;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.CircularImage;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

public class WriteInScenic
{
	/** 当前View **/
	View view;
	View topView;
	MoMoRefreshListView listView;
	FeatureDiaryAdapter adapter;
	BaseApplication application;
	Context context;
	private List<Entity> features;
	public CircularImage avatar;
	protected FinalBitmap fb;
	private ImageView img;
	private ImageButton write;
	OnClickListener clickListener;
	Button loading;
	OnRefreshListener onRefreshListener;
	OnCancelListener onCancelListener;
	public LinearLayout noLoading;
	public TextView userName;
	Activity activity;

	public WriteInScenic(BaseApplication application, Context context,
			Activity activity, List<Entity> datas,
			OnClickListener clickListener, boolean isLoading,
			OnRefreshListener onRefreshListener,
			OnCancelListener onCancelListener)
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.refresh_listview, null);
		topView = inflater.inflate(R.layout.activity_personage, null);
		fb = FinalBitmap.create(context);
		this.activity = activity;
		this.onRefreshListener = onRefreshListener;
		this.onCancelListener = onCancelListener;
		this.clickListener = clickListener;
		this.application = application;
		this.context = context;
		this.features = datas;
		initViews();
		if (!isLoading)
		{
			noLoading.setVisibility(View.VISIBLE);
		}
	}

	public void initViews()
	{
		noLoading = (LinearLayout) view.findViewById(R.id.not_loading);
		loading = (Button) view.findViewById(R.id.not_loading_but);
		write = (ImageButton) topView.findViewById(R.id.strategy_write);
		avatar = (CircularImage) topView.findViewById(R.id.personge_avatar);
		listView = (MoMoRefreshListView) view.findViewById(R.id.refresh_list);
		img = (ImageView) topView.findViewById(R.id.personge_bg);
		userName = (TextView) topView.findViewById(R.id.userName);
		adapter = new FeatureDiaryAdapter(application, context, features, 0);
		listView.addHeaderView(topView);
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(onRefreshListener);
		listView.setOnCancelListener(onCancelListener);
		write.setOnClickListener(clickListener);
		loading.setOnClickListener(clickListener);
		avatar.setOnClickListener(clickListener);
		application.imageLoader
				.displayImage(
						"http://e.hiphotos.baidu.com/image/w%3D1366%3Bcrop%3D0%2C0%2C1366%2C768/sign=ddd2cd13c45c1038247ecac18427a870/622762d0f703918f946ef0c3533d269759eec478.jpg",
						img, application.options);
		fb.display(avatar, "http://117.21.209.117:9000/driving/files/user.jpg");
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (arg2 > 1)
				{
					MyArticle article = (MyArticle) features.get(arg2 - 2);
					post(article.getId());
				}
			}
		});
	}

	public View getView()
	{
		return view;
	}

	public void post(String id)
	{
		Utils.showLoadingDialog(context, "数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "19");
		params.put("id", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONObject object = new JSONObject(json);
					String id = object.getString("id");
					String content = object.getString("content");
					String title = object.getString("title");
					String uid = object.getString("uid");
					String uname = object.getString("uname");
					String times = object.getString("times");
					String laud = object.getString("laud");
					String marrow = object.getString("marrow");
					String clicks = object.getString("clicks");
					String img0 = null;
					String img1 = null;
					String img2 = null;
					if (object.has("img0"))
					{
						img0 = object.getString("img0");
					}
					if (object.has("img1"))
					{
						img1 = object.getString("img1");
					}
					if (object.has("img2"))
					{
						img2 = object.getString("img2");
					}
					application.myArticleDetails = new MyArticleDetails(id,
							content, title, uid, uname, times, laud, marrow,
							clicks, img0, img1, img2);
					Utils.dismissLoadingDialog();
					Intent intent = new Intent(WriteInScenic.this.context,
							AboutMeMyArticleDetails.class);
					WriteInScenic.this.context.startActivity(intent);
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

}
